//
//  ALStatusCell.swift
//  ALSinaWeiBoSwift
//
//  Created by 柴瑞军 on 16/4/14.
//  Copyright © 2016年 myCompany. All rights reserved.
//

import UIKit
import SnapKit
class ALStatusCell: UITableViewCell {
    var btmVConstrains :Constraint?

    var status: ALStatus?{
//        首先要对约束进行一次卸载操作
        
        didSet{
          originalView.status = status
            /**
             在进行设置被转发的数据的时候首先要判断有没有被转发的数据内容,如果有在进行赋值
             
             
             当然;这也涉及到约束的问题:所以在此处要先用一个属性来记录着当前的约束,在进行判断以后再对约束进行更新操作
             */
            btmVConstrains?.uninstall()
            if let retweekStatus = status?.retweeted_status {
                retweetView.hidden = false
                retweetView.retweekStatus = retweekStatus
                
                bottomView.snp_makeConstraints(closure: { (make) in
                    btmVConstrains = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else{
                retweetView.hidden = true
                bottomView.snp_makeConstraints(closure: { (make) in
                    btmVConstrains = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    
//自定义的cell可以重写下面的方法实现添加子控件
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
//        调用父类的方法
        super.init(style: style, reuseIdentifier: reuseIdentifier)
//        设置cell的背景颜色
        backgroundColor  = UIColor.randomColor()
        // 设置cell背景颜色
//        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
//contentView.backgroundColor = UIColor.randomColor()
//        添加子控件
        preparaUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func preparaUI(){
    
//        将自定义的view添加到contentView上
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
//        设置约束
        originalView.snp_makeConstraints{ (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
//        设置子控件的高度随机
//            make.height.equalTo(Int(arc4random_uniform(200))+50)
        }
        retweetView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            // TODO: 测试
            //            make.height.equalTo(200)
            // 实际上被转发view的高度需要参考自己被转发的配图
//            make.height.equalTo(CGFloat(arc4random_uniform(200))+80)
        }
        
        bottomView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            
            make.height.equalTo(37)
            
            btmVConstrains = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        
//        设置contentView的约束:,首先一点要设置tableview的行高的属性为automic,设置预估行高,在cell进行展示的时候才去计算真是的行高,contentView的高度约束要参考子控件
        contentView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
        
        
        //图层异步绘制?
        self.layer.drawsAsynchronously = true
        
        //栅格化:将view截图成一个bitmap,只要view的内用不变化,则就可以直接使用bitmap,会产生离屏渲染
        
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
    }
//    懒加载自定义的view
   private lazy var originalView : ALStatusOriginalView = ALStatusOriginalView()
//    懒加载bottomView
   private lazy var bottomView: ALStatusBottomView = ALStatusBottomView()
//    被转发的view
   private lazy var retweetView: ALStatusRetweetView = ALStatusRetweetView()
}
